<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Mapping Search</title>

<LINK REL="stylesheet" HREF="CSS/Main.css" TYPE="text/css">
<LINK REL="stylesheet" HREF="CSS/Button.css" TYPE="text/css">
</head>
<body>
	<div id="top">
		<h1>Master Data Management</h1>
	</div>

	<form action="MappingServlet" method="get">
		<h2>Map By</h2>
		<select name="type">
			<option>Commodity</option>
			<option>Market</option>
			<option>Location</option>
			<option>Currency</option>
		</select>


		<!-- 
	<Table>
		<tr>
			<td>Location: </td>
			<td><input type="text" name="Location"></td>
			
			<td>Currency: </td>
			<td><input type="text" name="Currency"></td>
			
			<td>Market: </td>
			<td><input type="text" name="Market"></td>
			
			<td>Commodity: </td>
			<td><input type="text" name="Commodity"></td>
		</tr>
	</Table>
 -->
		<input type="submit" class="button" name="submit" value="submit">
	</form>
	<br />

	<%
		String type = (String) request.getAttribute("Type");
	%>
	<%
		if (type != null) {
	%>
	<h5>
		Mapping by
		<%=type%></h5>
	<form action="MappingServlet" method="post" name="frm">
		<Table>
			<tr>
				<td>Mapping Entity</td>
				<td><input type="text" name="Entity" id="mapentity"
					onkeydown="checkMapEntity()" autofocus required></td>
				<p id="p1"></p>
			</tr>
			<tr>
				<td>Mapping String</td>
				<td><input type="text" name="String" id="mapstring"
					onkeydown="checkMapString()" required></td>
			</tr>
			<tr>
				<td>Mapping Description</td>
				<td><input type="text" name="Description" id="mapdesc"
					onkeydown="checkMapDesc()" required></td>
			</tr>
			<tr>
				<td>Start Date</td>
				<td><input type="date" name="StartDate" value="01/01/2004"
					required></td>
			</tr>
			<tr>
				<td>End Date</td>
				<td><input type="date" name="EndDate" ></td>
			</tr>
			<tr>
				<td>Source System</td>
				<td><input type="text" name="Source" id="sourceSystem"
					onkeydown="checkSourceSystem()" required></td>
			</tr>
		</Table>


		<input type="submit" name="submit" class="button" value="Submit"
			onclick="clicked();return(x&&xdate);">

	</form>
	<%
		} else {
		}
	%>

	<%
		if (session.getAttribute("role").equals("admin")) {
	%>
	<form action="Home.jsp">
		<input type="submit" class="button" name="Home" value="Home">
	</form>
	<%
		} else {
	%>
	<form action="HomeUser.jsp">
		<input type="submit" class="button" name="Home" value="Home">
	</form>
	<%
		}
	%>
	<script type="text/javascript">
		var check = 0;
		var flag1 = 0;//Mapping Entity
		var flag2 = 0;//Mapping String
		var flag3 = 0;//Mapping Desc
		var flag4 = 0;//Source System

		var x = true;
		var xdate = true;
		function checkMapEntity() {
			var txt = document.getElementById("mapentity");
			var len = (txt.value).length;
			if (len > 100) {
				flag1 = 1;
				x = false;
			} else {
				x = true;
				flag1 = 0;
			}
		}

		function checkMapString() {
			var txt = document.getElementById("mapstring");
			var len = (txt.value).length;
			if (len > 100) {
				flag2 = 1;
				x = false;
			} else {
				x = true;
				flag2 = 0;
			}
		}

		function checkMapDesc() {
			var txt = document.getElementById("mapdesc");
			var len = (txt.value).length;
			if (len > 100) {
				flag3 = 1;
				x = false;
			} else {
				x = true;
				flag3 = 0;
			}
		}

		function checkSourceSystem() {
			var txt = document.getElementById("sourceSystem");
			var len = (txt.value).length;
			if (len > 100) {
				flag4 = 1;
				x = false;
			} else {
				x = true;
				flag4 = 0;
			}
		}
		/* 
		 var separator= "/";
		 var minYear=1900;
		 var maxYear=2500;

		 function isInteger(s){
		 var i;
		 for (i = 0; i < s.length; i++){ 
		 // Check that current character is a number or not.
		 var c = s.charAt(i);
		 if (((c < "0") || (c > "9"))) return false;
		 }
		 // All characters are numbers.
		 return true;
		 }

		 function stripCharsInBag(s, bag){
		 var i;
		 var returnString = "";
		 // Search through string's characters one by one.
		 // If character is not in bag, append to returnString.
		 for (i = 0; i < s.length; i++){ 
		 var c = s.charAt(i);
		 if (bag.indexOf(c) == -1) returnString += c;
		 }
		 return returnString;
		 }

		 function daysInFebruary (year){
		 // February has 29 days in any year evenly divisible by four,
		 // EXCEPT for centurial years which are not also divisible by 400.
		 return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
		 }
		 function DaysArray(n) {
		 for (var i = 1; i <= n; i++) {
		 this[i] = 31
		 if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		 if (i==2) {this[i] = 29}
		 } 
		 return this
		 }

		 function isDate(dtStr){
		 var daysInMonth = DaysArray(12)
		 var pos1=dtStr.indexOf(separator)
		 var pos2=dtStr.indexOf(separator,pos1+1)
		 var strMonth=dtStr.substring(0,pos1)
		 var strDay=dtStr.substring(pos1+1,pos2)
		 var strYear=dtStr.substring(pos2+1)
		 strYr=strYear
		 if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
		 if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
		 for (var i = 1; i <= 3; i++) {
		 if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
		 }
		 month=parseInt(strMonth)
		 day=parseInt(strDay)
		 year=parseInt(strYr)
		 if (pos1==-1 || pos2==-1){
		 alert("The date format should be : MM/DD/YYYY")
		 return false
		 }
		 if (strMonth.length<1 || month<1 || month>12){
		 alert("Please enter a valid month")
		 return false
		 }
		 if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		 alert("Please enter a valid day")
		 return false
		 }
		 if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		 alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		 return false
		 }
		 if (dtStr.indexOf(separator,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, separator))==false){
		 alert("Please enter a valid date")
		 return false
		 }
		 //alert("Entered date is valid")
		 return true
		 }

		 function ValidateDate(){
		 var dt=document.frm.StartDate;
		 if (isDate(dt.value)==false){
		 dt.focus()
		 return false
		 }
		 return true
		 } */

		function clicked() {
			var dt = document.frm.StartDate.value;
			var dt1 = document.frm.EndDate.value;

			if (dt1 != "") {
				if (dt > dt1) {
					alert("Start date cannot be greater than End date")
					xdate = false
				} else {
					xdate = true
				}
			}
			if (flag1 == 1) {
				alert("Mapping Entity Cant be more than 100 characters");
				x = false;
			}
			if (flag2 == 1) {
				alert("Mapping String Cant be more than 100 characters");
				x = false;
			}

			if (flag3 == 1) {
				alert("Mapping Description Cant be more than 100 characters");
				x = false;
			}
			if (flag4 == 1) {
				alert("Source System Cant be more than 100 characters");
				x = false;
			}

		}}
	</script>
</body>
</html>
